package servlet;

import java.io.IOException;

import entities.Usuario;
import fachada.Fachada;
import interfaceFachada.IFachada;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Logar extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5137937517832337025L;

	private IFachada fachada;
	public void DoPost(HttpServletRequest request, HttpServletResponse response)
	{
		fachada = Fachada.getFachada();
		
		String login = (String) request.getAttribute("login");
		String senha = (String) request.getAttribute("senha");
		Usuario usuario = new Usuario();
		usuario.setLogin(login);
		usuario.setSenha(senha);
		if(fachada.efetuarLogin(usuario))
		{
			HttpSession session = request.getSession(true);
			session.setAttribute("UsuarioLogado", usuario);
			try {
				response.sendRedirect("logado.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			try {
				response.sendRedirect("index.html");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
